package com.beiming.moudle.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beiming.common.utils.Result;
import com.beiming.common.utils.TokenUtil;
import com.beiming.moudle.auth.entity.BussEntity;
import com.beiming.moudle.auth.entity.CustomerEntity;
import com.beiming.moudle.auth.entity.CustomerUserEntity;
import com.beiming.moudle.auth.entity.utillClass.BussPage;
import com.beiming.moudle.auth.entity.utillClass.CustomerPage;
import com.beiming.moudle.auth.entity.utillClass.User;
import com.beiming.moudle.auth.service.CustomerService;
import com.beiming.moudle.auth.service.CustomerUserService;
import com.beiming.moudle.auth.vo.TokenVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户信息
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    private final CustomerService customerService;
    private final CustomerUserService customerUserService;

    public CustomerController(CustomerService customerService, CustomerUserService customerUserService) {
        this.customerService = customerService;
        this.customerUserService = customerUserService;
    }

    @PostMapping("/login")
    public Result login(@RequestBody User u) {
        System.out.println(u);
        CustomerUserEntity user = customerUserService.findByUsername(u.getUsername());
        //账号不存在、密码错误
        if (user == null || !user.getPassword().equals(u.getPassword())) {
            return Result.build(400, "用户名或密码错误");
        } else {
            //生成token，并保存到数据库
            String token = customerUserService.createToken(user);
            TokenVO tokenVO = new TokenVO();
            tokenVO.setToken(token);
            tokenVO.setUsername(user.getLoginName());
            tokenVO.setId(user.getCustomerId());
            return Result.ok(tokenVO);
        }
    }
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        //从request中取出token
        String token = TokenUtil.getRequestToken(request).get("user_token").toString();
        System.out.println("========"+token);
        customerUserService.logout(token);
        return Result.ok();
    }
    @PostMapping("/register")
    public Result register(@RequestBody CustomerUserEntity user){
        user.setUserStats(1);
       customerUserService.addCustomerUser(user);
        return Result.ok();
    }
    @PostMapping("/info")
    public Result getMsgPage(@RequestBody CustomerPage cuPage){
        IPage<CustomerEntity> customerEntityIPage = customerService.getMsgPage(cuPage);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( cuPage.getCurrentPage() > customerEntityIPage.getPages()){
            cuPage.setCurrentPage((int) customerEntityIPage.getPages());
            customerEntityIPage = customerService.getMsgPage(cuPage);
        }
        List<CustomerEntity> customerEntities = customerEntityIPage.getRecords();
        for(CustomerEntity customerEntity : customerEntities){
            customerEntity.setUserStats(customerUserService.getUserStatus(customerEntity.getCustomerId()));
        }
        return Result.ok(customerEntityIPage);
    }
    @PostMapping("/updataCustomerStatus")
    private Result setStopStatus(@RequestBody CustomerUserEntity customerUserEntity){
        System.out.println(customerUserEntity);
        int i= customerUserService.updateCustomerStatus(customerUserEntity);
        return Result.ok(i);
    }
}
